package com.excel.validators.test.intering;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.regex.Pattern;

import org.junit.Test;

import com.excel.validators.core.exception.InstanceException;
import com.excel.validators.excel.entrance.ExcelHandler;

public class IntelingImport {

	String infile = "/data/share/inteling/cnce.xlsx";
	String internetation = "/data/share/inteling/test/locale-int.properties";
	String localetmp = "/data/share/inteling/test/locale-tmp";
	String localeoutput = "/data/share/inteling/test/create/";

	/**
	 * 导入excel测试
	 * @throws IOException 
	 */
	@Test
	public void importCnce() throws InstanceException, IOException {
		ExcelHandler handler = new ExcelHandler();
		// 主导航翻译
		final Map<String, Navagation> navagations = new HashMap<>();
		handler.importExcel("navagation", new File(infile), 1).getModelObject(Navagation.class)
		.stream().forEach(n -> {
			navagations.put(n.getCode(), n);
		});
		
		// 底部翻译
		final Map<String, Floor> floors = new HashMap<>();
		handler.importExcel("floor", new File(infile), 2).getModelObject(Floor.class)
		.stream().forEach(f -> {
			floors.put(f.getCode(), f);
		});
		
		// 首页翻译
		final Map<String, Home> homes = new HashMap<>();
		handler.importExcel("home", new File(infile), 3).getModelObject(Home.class)
		.stream().forEach(h -> {
			homes.put(h.getCode(), h);
		});	
		
		// 首页翻译
		final Map<String, ViewList> listviews = new HashMap<>();
		handler.importExcel("viewlist", new File(infile), 4).getModelObject(ViewList.class)
		.stream().forEach(v -> {
			listviews.put(v.getCode(), v);
		});			
		
		// 首页翻译
		final Map<String, ProductDetail> products = new HashMap<>();
		handler.importExcel("productsdetail", new File(infile), 5).getModelObject(ProductDetail.class)
		.stream().forEach(p -> {
			products.put(p.getCode(), p);
		});
		
		// icon 图标
		final Map<String, String> icons = new HashMap<>();
		Properties properties = new Properties();
		try {
			properties.load(new FileInputStream(internetation));
			Pattern pattern = Pattern.compile("locale.worldwide.view.(\\w{2})$");
			for (Entry<Object, Object> entity : properties.entrySet()) {
				String key = entity.getKey().toString().trim();
				if (pattern.matcher(key).find()) {
					String code = entity.getValue().toString().trim();
					String icon = properties.get(key.concat(".image")).toString();
					icons.put(code, icon);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		// 写文件逻辑
		byte[] bytes = Files.readAllBytes(Paths.get(localetmp));
		String template = new String(bytes, Charset.forName("UTF-8"));
		
		String message = "";
		try {
			// 迭代57种语言
			for (String code : icons.keySet()) {
				message = code;
				String newTemplate = template.replace("{country}", navagations.get(code).getName())
					.replace("{language.name}", code)
					.replace("{language.icon}", icons.get(code))
					.replace("{home.ourproducts}", homes.get(code).getOurproducts())
					.replace("{home.ournews}", homes.get(code).getOurnews())
					.replace("{list.viewmore}", listviews.get(code).getViewmorelist())
					.replace("{home.viewmorenews}", homes.get(code).getViewmorenews())
					.replace("{home.viewmoreproducts}", homes.get(code).getViewmoreproducts())
					.replace("{navagation.home}", navagations.get(code).getHome())
					.replace("{navagation.products}", navagations.get(code).getProducts())
					.replace("{navagation.news}", navagations.get(code).getNews())
					.replace("{navagation.about}", navagations.get(code).getAbout())
					.replace("{navagation.contact}", navagations.get(code).getContact())
					.replace("{navagation.name}", floors.get(code).getNavagation())
					.replace("{floor.worldwide}", floors.get(code).getWorldwide())
					.replace("{product.description}", products.get(code).getDescription())
					.replace("{contact.title}", floors.get(code).getContact())
					.replace("{submit.message}", floors.get(code).getMessage())
					.replace("{submit.send}", floors.get(code).getSend())
					.replace("{submit.yourname}", floors.get(code).getYourname())
					.replace("{submit.email}", floors.get(code).getEmail())
					.replace("{submit.yournumber}", floors.get(code).getYournumber())
					.replace("{submit.name}", floors.get(code).getSubmit())
					.replace("{floor.home}", floors.get(code).getHome())
					.replace("{floor.about}", floors.get(code).getAbout())
					.replace("{floor.news}", floors.get(code).getNews())
					.replace("{floor.products}", floors.get(code).getProducts())
					.replace("{floor.contact}", floors.get(code).getContact())
					.replace("{floor.worldwide}", floors.get(code).getWorldwide());
				
				File file = new File(localeoutput.concat("locale-").concat(code).concat(".properties"));
				if (!file.exists()) file.createNewFile();
				Files.write(Paths.get(file.toString()), newTemplate.getBytes());
			}			
		} catch (Exception e) {
			System.out.println("error: " + message);
			e.printStackTrace();
		}
	}
	
}
